#include "comLogManager.h"
#include "InfoSpaceMgr.h"
#include "ttcodeStateChart.h"

#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>


InfoSpaceMgr::InfoSpaceMgr( wxWindow* parent )
:PanelInfoSpace(parent)
,m_InfoCtrl(NULL)
{
	m_InfoCtrl = new InfoSpaceCtrl(m_auinotebookInfoSpace,"all log");
}







InfoSpaceCtrl::InfoSpaceCtrl(  wxAuiNotebook *parent,const std::string &name )
: wxStyledTextCtrl(parent)
{
	parent->AddPage(this,name,true,wxNullBitmap);
}

void InfoSpaceCtrl::OnInternalIdle(void)
{
//	ttcode_log << endl;
	ReportInfoList info;
	globalCollectMgr->getTopReport(info);
	BOOST_FOREACH(std::string item,info)
	{
		wxString disp( item);
		if( disp.empty() )
		{
			continue;
		}
		this->AppendText(disp + "\n");
		this->GotoLine(this->GetLineCount());
	}
}

BEGIN_EVENT_TABLE(InfoSpaceCtrl, wxStyledTextCtrl)
	EVT_STC_DOUBLECLICK( wxID_ANY,InfoSpaceCtrl::onDoubleClick)
END_EVENT_TABLE()

void InfoSpaceCtrl::onDoubleClick(wxStyledTextEvent& evt)
{
	int lineNum = this->GetCurrentLine();
	wxString textLine = this->GetLine(lineNum);
	boost::smatch what;
	std::string regInput(textLine.c_str());
	if( boost::regex_search(regInput,what, boost::regex(":\\d+:\\d+:")) )
	{
//		ttcode_log << "lineNum=<"<< lineNum << ">" << endl;
//		ttcode_log << "textLine=<"<< textLine << ">" << endl;
		string delimit(what[0]);
//		ttcode_log << "delimit=<"<< delimit << ">" << endl;
		size_t pos = regInput.find(delimit);
		if ( string::npos != pos )
		{
			string filepath = regInput.substr(0,pos);
			string content = regInput.substr( pos + delimit.size() );
//			ttcode_log << "filepath=<"<< filepath << ">" << endl;
//			ttcode_log << "content=<"<< content << ">" << endl;
			wxString msgLine = this->GetLine(lineNum+1);
			msgLine +=  this->GetLine(lineNum+2);
//			ttcode_log << "msgLine=<"<< msgLine << ">" << endl;
			
			eventFillReportDirect evt;
			evt.filepath = filepath;
			evt.pos = delimit;
			evt.content = content;
			evt.tips = string(msgLine.c_str());
			globalStateChart.process_event( evt );
		}
	}
}
